unit ConfirmLink;

interface

uses
  Database, NewMap, AM,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls, ColorGrd, Mask,
  Menus, ComCtrls, DBCtrls, JvExMask, JvSpin, JvDBSpinEdit;

type
  TFormConfirmLink = class(TForm)
    BtnOK: TBitBtn;
    BtnCancel: TBitBtn;
    PanelMain: TPanel;
    Label1a: TLabel;
    Label1b: TLabel;
    Edit1: TEdit;
    Label2a: TLabel;
    Label2b: TLabel;
    Bevel1: TBevel;
    Edit2: TEdit;
    PanelO: TPanel;
    PanelRoom1: TPanel;
    b0: TSpeedButton;
    b1: TSpeedButton;
    b2: TSpeedButton;
    b3: TSpeedButton;
    b4: TSpeedButton;
    Colors1: TColorGrid;
    CheckO: TCheckBox;
    PanelD: TPanel;
    CheckD: TCheckBox;
    PanelRoom2: TPanel;
    c0: TSpeedButton;
    c1: TSpeedButton;
    c2: TSpeedButton;
    c3: TSpeedButton;
    c4: TSpeedButton;
    Colors2: TColorGrid;
    PanelLink: TPanel;
    Cover2: TPanel;
    Label8: TLabel;
    Label12: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    ColorLink: TColorGrid;
    Label7: TLabel;
    Bevel2: TBevel;
    Bevel5: TBevel;
    r1: TImage;
    r2: TImage;
    r3: TImage;
    r4: TImage;
    d1: TImage;
    d2: TImage;
    Bevel3: TBevel;
    PanelExample: TPanel;
    Shape1: TShape;
    Link: TShape;
    Example1: TShape;
    Example2: TShape;
    Room1: TImage;
    Room2: TImage;
    dir: TImage;
    Label1: TLabel;
    EditPre1: TEdit;
    EditPost1: TEdit;
    EditPre2: TEdit;
    EditPost2: TEdit;
    Label3: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    BtnHelp: TBitBtn;
    MainMenu1: TMainMenu;
    Main1: TMenuItem;
    MenuHelp: TMenuItem;
    BtnAM: TBitBtn;
    BtnAM2: TBitBtn;
    CtrlA1: TMenuItem;
    BtnAM4: TBitBtn;
    BtnAM3: TBitBtn;
    Panel1: TPanel;
    Status: TLabel;
    BtnNewMap: TBitBtn;
    r5: TImage;
    b5: TSpeedButton;
    c5: TSpeedButton;
    X1: TJvSpinEdit;
    Y1: TJvSpinEdit;
    X2: TJvSpinEdit;
    Y2: TJvSpinEdit;
    EditMap1: TComboBox;
    EditMap2: TComboBox;
    Cover1: TPanel;
    procedure BtnCancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure BtnOKClick(Sender: TObject);
    procedure CheckOClick(Sender: TObject);
    procedure X1Exit(Sender: TObject);
    procedure EditMap2Change(Sender: TObject);
    procedure Colors1Change(Sender: TObject);
    procedure X1Change(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure BtnHelpClick(Sender: TObject);
    procedure BtnAMClick(Sender: TObject);
    procedure BtnNewMapClick(Sender: TObject);
  private
    { Private declarations }
    procedure UpdateExamples;
  public
    { Public declarations }
    linked: boolean;
    procedure UpdateEnabling;
    function FigureRoom(which: integer): integer;
  end;

implementation

uses Map, DBClient;

{$R *.DFM}

procedure TFormConfirmLink.FormCreate(Sender: TObject);
var table: TClientDataset;
begin
  linked:=false;
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableMaps, true);
    active:=true;
    first;
    while not EOF do begin
      EditMap1.items.add(fieldbyname('Map').asstring);
      next;
    end;
  end;
  table.free;
  EditMap2.items.assign(EditMap1.items);
end;

procedure TFormConfirmLink.BtnCancelClick(Sender: TObject);
begin
  linked:=false;
  close;
end;

procedure TFormConfirmLink.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    Edit2.selectall;
    Edit2.setfocus;
    key:=#0;
  end;
end;

procedure TFormConfirmLink.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    BtnOK.click;
    key:=#0;
  end;
end;

procedure TFormConfirmLink.BtnOKClick(Sender: TObject);
var
  table: TCustomClientDataset;
begin
  if (Edit1.text = '') and (Edit2.text = '') then begin
    messagedlg('You must have at least one exit listed',
      mtinformation,[mbok],0);
    Edit1.setfocus;
    exit;
  end;
  EditMap1.text:=uppercase(Datawell.CheckAllSpaces(EditMap1.text));
  EditMap2.text:=uppercase(Datawell.CheckAllSpaces(EditMap2.text));
  if EditMap1.text = '' then begin
    messagedlg('You need to select a map level for the originating room',
      mtinformation,[mbok],0);
    EditMap1.setfocus;
    exit;
  end;
  if EditMap2.text = '' then begin
    messagedlg('You need to select a map level for the destination room',
      mtinformation,[mbok],0);
    EditMap2.setfocus;
    exit;
  end;
  table:=Datawell.TableMaps;
  if not table.Locate('map', EditMap1.Text, []) then
    Exit;
  if not ((EditMap1.Text = EditMap2.Text) or (table.Locate('map', EditMap2.Text, []))) then
    Exit;
  Edit1.text:=Datawell.CheckAllSpaces(Edit1.text);
  Edit2.text:=Datawell.CheckAllSpaces(Edit2.text);
  if (EditMap1.text = EditMap2.text) and (X1.value = X2.value) and
    (Y1.value = Y2.value) then begin
    if messagedlg('The origination and destination rooms are the same.  Continue?',
      mtconfirmation,[mbyes,mbcancel],0) = mrcancel then exit;
  end;
  linked:=true;
  close;
end;

procedure TFormConfirmLink.CheckOClick(Sender: TObject);
begin
  UpdateExamples;
  UpdateEnabling;
end;

procedure TFormConfirmLink.UpdateEnabling;
begin
  if (CheckO.checked) and (CheckO.enabled) then begin
    PanelRoom1.enabled:=true;
    EditMap1.enabled:=true;
    EditMap1.tabstop:=true;
    EditMap1.color:=clwindow;
    Cover1.hide;
  end
  else begin
    PanelRoom1.enabled:=false;
    EditMap1.enabled:=false;
    EditMap1.tabstop:=false;
    EditMap1.color:=clsilver;
    Cover1.show;
  end;
  X1.enabled:=EditMap1.enabled;
  X1.tabstop:=EditMap1.tabstop;
  X1.color:=EditMap1.color;
  Y1.enabled:=EditMap1.enabled;
  Y1.tabstop:=EditMap1.tabstop;
  Y1.color:=EditMap1.color;
  if (CheckD.checked) and (CheckD.enabled) then begin
    PanelRoom2.enabled:=true;
    EditMap2.enabled:=true;
    EditMap2.tabstop:=true;
    EditMap2.color:=clwindow;
    Cover2.hide;
  end
  else begin
    PanelRoom2.enabled:=false;
    EditMap2.enabled:=false;
    EditMap2.tabstop:=false;
    EditMap2.color:=clsilver;
    Cover2.show;
  end;
  X2.enabled:=EditMap2.enabled;
  X2.tabstop:=EditMap2.tabstop;
  X2.color:=EditMap2.color;
  Y2.enabled:=EditMap2.enabled;
  Y2.tabstop:=EditMap2.tabstop;
  Y2.color:=EditMap2.color;
end;

function TFormConfirmLink.FigureRoom(which: integer): integer;
begin
  case which of
    1: begin
         result:=-1;
         if b0.down then result:=0
         else if b1.down then result:=1
         else if b2.down then result:=2
         else if b3.down then result:=3
         else if b4.down then result:=4
         else if b5.down then result:=5;
       end;
    2: begin
         result:=-1;
         if c0.down then result:=0
         else if c1.down then result:=1
         else if c2.down then result:=2
         else if c3.down then result:=3
         else if c4.down then result:=4
         else if c5.down then result:=5;
       end;
    else raise Exception.CreateFmt('TFormConfirmLink.FigureRoom: Invalid room number: %d', [which]);
  end;
end;

procedure TFormConfirmLink.X1Exit(Sender: TObject);
begin
  if TJvDBSpinEdit(sender).value < -9999 then TJvDBSpinEdit(sender).value:=-9999;
  if TJvDBSpinEdit(sender).value > 9999 then TJvDBSpinEdit(sender).value:=9999;
end;

procedure TFormConfirmLink.EditMap2Change(Sender: TObject);
begin
  X2.value:=X1.value;
  Y2.value:=Y1.value;
  UpdateExamples;
end;

procedure TFormConfirmLink.Colors1Change(Sender: TObject);
begin
  UpdateExamples;
  X1Change(nil);
end;

procedure TFormConfirmLink.UpdateExamples;
var img: TImage;
begin
  if CheckO.checked then begin
    Example1.pen.color:=Datawell.FigureColor(Colors1.foregroundindex);
    Example1.brush.color:=Datawell.FigureColor(Colors1.backgroundindex);
    if b0.down then Room1.hide
    else begin
      if b1.down then img:=r1
      else if b2.down then img:=r2
      else if b3.down then img:=r3
      else if b4.down then img:=r4
      else if b5.down then img:=r5
      else raise Exception.Create('TFormConfirmLink.UpdateExamples: No image button is pressed');
      with Room1.canvas do begin
        brush.color:=Example1.brush.color;
        BrushCopy(Bounds(0,0,13,13),img.picture.bitmap,Bounds(0,0,13,13),clwhite);
        brush.color:=Example1.pen.color;
        floodfill(6,6,clblack,fssurface);
      end;
      Room1.show;
    end;
    Example1.show;
  end
  else begin
    Room1.hide;
    Example1.hide;
  end;
  if CheckD.checked then begin
    Example2.pen.color:=Datawell.FigureColor(Colors2.foregroundindex);
    Example2.brush.color:=Datawell.FigureColor(Colors2.backgroundindex);
    if c0.down then Room2.hide
    else begin
      if c1.down then img:=r1
      else if c2.down then img:=r2
      else if c3.down then img:=r3
      else if c4.down then img:=r4
      else if c5.down then img:=r5
      else raise Exception.Create('TFormConfirmLink.UpdateExamples: None of the required buttons are down');
      with Room2.canvas do begin
        brush.color:=Example2.brush.color;
        BrushCopy(Bounds(0,0,13,13),img.picture.bitmap,Bounds(0,0,13,13),clwhite);
        brush.color:=Example2.pen.color;
        floodfill(6,6,clblack,fssurface);
      end;
      Room2.show;
    end;
    Example2.show;
  end
  else begin
    Room2.hide;
    Example2.hide;
  end;
  link.pen.color:=Datawell.FigureColor(ColorLink.foregroundindex);
  if (Edit1.text = '') and (Edit2.text = '') then link.hide
  else if (Edit1.text <> '') or (Edit2.text <> '') then link.show;
  if EditMap1.text <> EditMap2.text then link.hide;
  img:=nil;
  if (Edit1.text = '') and (Edit2.text <> '') then img:=d1
  else if (Edit1.text <> '') and (Edit2.text = '') then img:=d2;
  if (img <> nil) and (EditMap1.text = EditMap2.text) then begin
    with dir.canvas do begin
      brush.color:=clwhite;
      BrushCopy(Bounds(0,0,13,13),img.picture.bitmap,Bounds(0,0,13,13),clwhite);
      brush.color:=link.pen.color;
      floodfill(6,6,clblack,fssurface);
    end;
    dir.show;
  end
  else dir.hide;
end;

procedure TFormConfirmLink.X1Change(Sender: TObject);
begin
  Label1a.caption:='&From ORIGINATING room ('+floattostr(X1.value)+','+
    floattostr(Y1.value)+')';
  Label1b.caption:='To DESTINATION room ('+floattostr(X2.value)+','+
    floattostr(Y2.value)+')';
  Label2a.caption:='From DESTINATION room ('+floattostr(X2.value)+','+
    floattostr(Y2.value)+')';
  Label2b.caption:='back &To ORGINATING room ('+floattostr(X1.value)+','+
    floattostr(Y1.value)+')';
end;

procedure TFormConfirmLink.FormMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormConfirmLink.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('create_map_link');
end;

procedure TFormConfirmLink.BtnAMClick(Sender: TObject);
var
  form: TFormAM;
  am: string;
  ed: TEdit;
begin
  if (TComponent(sender).tag = 999) then begin
    if ActiveControl = EditPre1 then ed:=EditPre1
    else if ActiveControl = EditPost1 then ed:=EditPost1
    else if ActiveControl = EditPre2 then ed:=EditPre2
    else if ActiveControl = EditPost2 then ed:=EditPost2
    else exit;
  end
  else begin
    case TComponent(sender).tag of
      1: ed:=EditPre1;
      2: ed:=EditPost1;
      3: ed:=EditPre2;
      4: ed:=EditPost2;
      else raise Exception.CreateFmt('TFormConfirmLink.BtnAMClick: invalid tag: %d', [TComponent(sender).tag]);
    end;
  end;
  form:=TFormAM.create(self);
  form.setup;
  form.showmodal;
  am:=form.am;
  form.free;
  if am = '' then exit;
  ed.text:=am;
  ed.setfocus;
end;

procedure TFormConfirmLink.BtnNewMapClick(Sender: TObject);
var
  form: TFormNewMap;
  didcreate: boolean;
  mapname: string;
begin
  form:=TFormNewMap.create(self);
  form.showmodal;
  didcreate:=form.didcreate;
  mapname:=form.mapname;
  form.free;
  if not didcreate then exit;
  EditMap2.items.add(mapname);
  EditMap1.items.add(mapname);
  EditMap2.itemindex:=EditMap2.items.indexof(mapname);
  EditMap2.text:=mapname;
  UpdateExamples;
end;

end.

